﻿using StudentManage.ViewModels.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using Zhy.DAL;
using Zhy.DAL.Models;
using Common;

namespace StudentManage.ViewModels
{
    public class StudentInfoViewModel : ViewModelBase
    {
        /// <summary>
        /// 操作数据库上下文
        /// </summary>
        ZhyDbContext context = new ZhyDbContext();
        public event Action ReloadList;//刷新事件
        string[] allInterestings = { "运动", "唱歌", "跳舞", "体育" };
        string oldName = "";
        string oldPhone = "";
        public StudentInfoViewModel()
        {
            stuInfo = new StudentInfo();
        }
        public StudentInfoViewModel(int stuId, int actType)
        {
            stuInfo = new StudentInfo();
            StuId = stuId;
            ActType = actType;
            if (stuId > 0)
            {
                stuInfo = context.StudentInfo.FirstOrDefault(p => p.StuId == stuId);
                oldName = stuInfo.StuName;
                oldPhone = stuInfo.Phone;
            }
            LoadInterestList();
        }

        /// <summary>
        /// 兴趣列表的加载
        /// </summary>
        private void LoadInterestList()
        {
            List<InterestModel> list = new List<InterestModel>();
            foreach (string str in allInterestings)
            {
                list.Add(new InterestModel() { ItemCheck = false, ItemName = str });
            }
            if (!string.IsNullOrEmpty(Interestings))
            {
                string[] newList = Interestings.Split(',');
                foreach (InterestModel item in list)
                {
                    if (newList.Contains(item.ItemName))
                    {
                        item.ItemCheck = true;
                    }
                }
            }
            InterestList = list;
        }

        /// <summary>
        /// 学生ID
        /// </summary>
        public int StuId
        {
            get { return stuInfo.StuId; }
            set
            {
                stuInfo.StuId = value;
                OnPropertyChanged();
            }
        }
        public int ActType { get; set; }
        private StudentInfo stuInfo;
        /// <summary>
        /// 学生信息
        /// </summary>
        public StudentInfo StuInfo
        {
            get { return stuInfo; }
            set { stuInfo = value; }
        }

        /// <summary>
        /// 学生名称
        /// </summary>
        public string StuName
        {
            get { return stuInfo.StuName; }
            set
            {
                stuInfo.StuName = value;
                OnPropertyChanged();
            }
        }

        /// <summary>
        /// 学生性别
        /// </summary>
        public string Sex
        {
            get { return stuInfo.Sex; }
            set
            {
                stuInfo.Sex = value;
                OnPropertyChanged();
            }
        }

        /// <summary>
        /// 学生性别单选框
        /// </summary>
        public bool IsMale
        {
            get { return stuInfo.Sex == "男" ? true : false; }
            set
            {
                stuInfo.Sex = value ? "男" : "女";
                OnPropertyChanged();
            }
        }
        /// <summary>
        /// 学生性别单选框
        /// </summary>
        public bool IsFemale
        {
            get { return stuInfo.Sex == "男" ? false : true; }
            set
            {
                stuInfo.Sex = value ? "女" : "男";
                OnPropertyChanged();
            }
        }
        /// <summary>
        /// 电话
        /// </summary>
        public string Phone
        {
            get { return stuInfo.Phone; }
            set
            {
                stuInfo.Phone = value;
                OnPropertyChanged();
            }
        }
        /// <summary>
        /// 班级ID
        /// </summary>
        public int ClassId
        {
            get { return stuInfo.ClassId; }
            set
            {
                stuInfo.ClassId = value;
                OnPropertyChanged();
            }
        }
        /// <summary>
        /// 兴趣爱好
        /// </summary>
        public string Interestings
        {
            get { return stuInfo.Interestings; }
            set
            {
                stuInfo.Interestings = value;
                OnPropertyChanged();
            }
        }

        /// <summary>
        /// 班级列表
        /// </summary>
        public List<ViewClassInfo> ClassList
        {
            get
            {
                List<ViewClassInfo> classList = context.ViewClassInfo.ToList();
                classList.Insert(0, new ViewClassInfo() { ClassId = 0, ClassName = "请选择" });
                return classList;
            }
        }

        private List<InterestModel> interestList = new List<InterestModel>();
        /// <summary>
        /// 兴趣列表
        /// </summary>
        public List<InterestModel> InterestList
        {
            get { return interestList; }
            set { interestList = value; }
        }
        /// <summary>
        /// 设置学生兴趣（添加或移除兴趣）
        /// </summary>
        public ICommand SetInterestingCmd
        {
            get
            {
                return new RelayCommand(o =>
                {
                    InterestModel model = o as InterestModel;
                    if (model != null)
                    {
                        List<string> strList = new List<string>();
                        if (!string.IsNullOrEmpty(Interestings))
                        {
                            strList = Interestings.GetStrList(',', false);
                        }
                        if (model.ItemCheck)
                        {
                            strList.Add(model.ItemName);
                        }
                        else
                        {
                            if (strList.Contains(model.ItemName))
                            {
                                strList.Remove(model.ItemName);
                            }
                        }
                        Interestings = string.Join(",", strList);
                    }
                });
            }
        }

        /// <summary>
        /// 确定按钮添加学生信息
        /// </summary>
        public ICommand ConfirmCmd
        {
            get
            {
                return new RelayCommand(o =>
                {
                    string typeName = ActType == 1 ? "新增" : "修改";
                    string msgTitle = $"学生{typeName}";
                    //检查信息
                    if (string.IsNullOrEmpty(StuName))
                    {
                        ShowError(msgTitle, "请输入学生姓名！");
                        return;
                    }
                    if (ClassId == 0)
                    {
                        ShowError(msgTitle, "请选择班级！");
                        return;
                    }
                    if (string.IsNullOrEmpty(Phone))
                    {
                        ShowError(msgTitle, "请输入学生电话！");
                        return;
                    }
                    if (ActType == 1 || (ActType == 2 && (oldPhone != Phone || oldName != StuName)))
                    {
                        if (context.StudentInfo.Count(p => p.StuName == StuName && p.Phone == Phone) > 0)
                        {
                            ShowError(msgTitle, "该学生信息已存在！");
                            return;
                        }
                    }
                    bool bl;
                    stuInfo.GradeId = 1;
                    stuInfo.Remark = "备注";
                    //提交
                    if (ActType == 1)
                    {
                        context.StudentInfo.Add(stuInfo);
                        context.SaveChanges();
                        bl = true;
                    }
                    else
                    {
                        context.StudentInfo.Update(stuInfo); 
                        context.SaveChanges();
                        bl = true;
                    }
                    if (bl)
                    {
                        ShowInfo(msgTitle, $"学生：{StuName} 的信息{typeName} 成功！");
                        ReloadList?.Invoke();
                    }
                    else
                    {
                        ShowError(msgTitle, $"学生：{StuName} 的信息{typeName} 失败！");
                        return;
                    }
                }, b =>
                {
                    return this.ActType == 4 ? false : true;
                });
            }


        }

        /// <summary>
        /// 关闭按钮
        /// </summary>
        public ICommand CloseWinCmd
        {
            get
            {
                return new RelayCommand(o =>
                {
                    if (ShowQuestion("关闭学生页", "你确定要关闭该页面吗？") == System.Windows.MessageBoxResult.Yes)
                    {
                        Close(o);
                    }
                });
            }
        }
    }
}
